home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / lib-src / getdate.y < prev    next >
Text File  |  1993-05-23  |  24KB  |  1,023 lines

  1. %{
  2. /*
  3. **  Originally written by Steven M. Bellovin <smb@research.att.com> while
  4. **  at the University of North Carolina at Chapel Hill.  Later tweaked by
  5. **  a couple of people on Usenet.  Completely overhauled by Rich $alz
  6. **  <rsalz@bbn.com> and Jim Berets <jberets@bbn.com> in August, 1990;
  7. **  send any email to Rich.
  8. **
  9. **  This grammar has nine shift/reduce conflicts.
  10. **
  11. **  This code is in the public domain and has no copyright.
  12. */
  13. /* SUPPRESS 287 on yaccpar_sccsid *//* Unusd static variable */
  14. /* SUPPRESS 288 on yyerrlab *//* Label unused */
  15.  
  16. #ifdef HAVE_CONFIG_H
  17. #include "config.h"
  18. #endif
  19.  
  20. /* Since the code of getdate.y is not included in the Emacs executable
  21.    itself, there is no need to #define static in this file.  Even if
  22.    the code were included in the Emacs executable, it probably
  23.    wouldn't do any harm to #undef it here; this will only cause
  24.    problems if we try to write to a static variable, which I don't
  25.    think this code needs to do.  */
  26. #ifdef emacs
  27. #undef static
  28. #endif
  29.  
  30. #ifdef __GNUC__
  31. #undef alloca
  32. #define alloca __builtin_alloca
  33. #else
  34. #ifdef HAVE_ALLOCA_H
  35. #include <alloca.h>
  36. #else
  37. #ifdef _AIX /* for Bison */
  38.  #pragma alloca
  39. #else
  40. char *alloca ();
  41. #endif
  42. #endif
  43. #endif
  44.  
  45. #include <stdio.h>
  46. #include <ctype.h>
  47.  
  48. /* The code at the top of get_date which figures out the offset of the
  49.    current time zone checks various CPP symbols to see if special
  50.    tricks are need, but defaults to using the gettimeofday system call.
  51.    Include <sys/time.h> if that will be used.  */
  52.  
  53. #if    defined(vms)
  54.  
  55. #include <types.h>
  56. #include <time.h>
  57.  
  58. #else
  59.  
  60. #include <sys/types.h>
  61.  
  62. #if sgi
  63. #undef timezone
  64. #endif
  65.  
  66. /* If the configuration process hasn't #define HAVE_GETTIMEOFDAY, try
  67.    to guess it.  */
  68. #ifndef HAVE_GETTIMEOFDAY
  69.  
  70. /* We know these machines have it.  */
  71. #ifdef BSD4_2
  72. #define HAVE_GETTIMEOFDAY
  73. #endif
  74.  
  75. #ifdef BSD4_1C
  76. #define HAVE_GETTIMEOFDAY
  77. #endif
  78.  
  79. #if (defined (hp9000) && !defined (hpux))
  80. #define HAVE_GETTIMEOFDAY
  81. #endif
  82.  
  83. #ifdef _AIX
  84. #define HAVE_GETTIMEOFDAY
  85. #endif
  86.  
  87. /* We know these machines don't.  We could just omit these, but some of
  88.    these CPP symbols are more specific than the ones above, so we want
  89.    to override them if any of the above are #defined.  */
  90. #ifdef USG
  91. #undef HAVE_GETTIMEOFDAY
  92. #endif
  93.  
  94. #ifdef sgi
  95. #undef HAVE_GETTIMEOFDAY
  96. #endif
  97.  
  98. #ifdef __386BSD__
  99. #undef HAVE_GETTIMEOFDAY
  100. #endif
  101.  
  102. #endif
  103.  
  104. #ifdef TIME_WITH_SYS_TIME
  105. #include <sys/time.h>
  106. #include <time.h>
  107. #else
  108. #ifdef HAVE_SYS_TIME_H
  109. #include <sys/time.h>
  110. #else
  111. #include <time.h>
  112. #endif
  113. #endif
  114.  
  115. #if defined(HAVE_SYS_TIMEB_H) || (!defined(USG) && defined(HAVE_FTIME))
  116. #include <sys/timeb.h>
  117. #else
  118. /*
  119. **  If you need to do a tzset() call to set the
  120. **  timezone, and don't have ftime().
  121. */
  122. struct timeb {
  123.     time_t        time;        /* Seconds since the epoch    */
  124.     unsigned short    millitm;    /* Field not used        */
  125.     short        timezone;
  126.     short        dstflag;    /* Field not used        */
  127. };
  128. #endif /* defined(HAVE_SYS_TIMEB_H) || (!defined(USG) && defined(HAVE_FTIME))*/
  129.  
  130. #endif    /* defined(vms) */
  131.  
  132. #if defined (STDC_HEADERS) || defined (USG)
  133. #include <string.h>
  134. #endif
  135.  
  136. /* Some old versions of bison generate parsers that use bcopy.
  137.    That loses on systems that don't provide the function, so we have
  138.    to redefine it here.  */
  139. #if !defined (HAVE_BCOPY) && defined (HAVE_MEMCPY) && !defined (bcopy)
  140. #define bcopy(from, to, len) memcpy ((to), (from), (len))
  141. #endif
  142.  
  143. extern struct tm    *localtime();
  144.  
  145. #define yyparse getdate_yyparse
  146. #define yylex getdate_yylex
  147. #define yyerror getdate_yyerror
  148.  
  149. static int yylex ();
  150. static int yyerror ();
  151.  
  152. #if    !defined(lint) && !defined(SABER)
  153. static char RCS[] =
  154.     "$Header: str2date.y,v 2.1 90/09/06 08:15:06 cronan Exp $";
  155. #endif    /* !defined(lint) && !defined(SABER) */
  156.  
  157.  
  158. #define EPOCH        1970
  159. #define HOUR(x)        ((time_t)(x) * 60)
  160. #define SECSPERDAY    (24L * 60L * 60L)
  161.  
  162.  
  163. /*
  164. **  An entry in the lexical lookup table.
  165. */
  166. typedef struct _TABLE {
  167.     char    *name;
  168.     int        type;
  169.     time_t    value;
  170. } TABLE;
  171.  
  172.  
  173. /*
  174. **  Daylight-savings mode:  on, off, or not yet known.
  175. */
  176. typedef enum _DSTMODE {
  177.     DSTon, DSToff, DSTmaybe
  178. } DSTMODE;
  179.  
  180. /*
  181. **  Meridian:  am, pm, or 24-hour style.
  182. */
  183. typedef enum _MERIDIAN {
  184.     MERam, MERpm, MER24
  185. } MERIDIAN;
  186.  
  187.  
  188. /*
  189. **  Global variables.  We could get rid of most of these by using a good
  190. **  union as the yacc stack.  (This routine was originally written before
  191. **  yacc had the %union construct.)  Maybe someday; right now we only use
  192. **  the %union very rarely.
  193. */
  194. static char    *yyInput;
  195. static DSTMODE    yyDSTmode;
  196. static time_t    yyDayOrdinal;
  197. static time_t    yyDayNumber;
  198. static int    yyHaveDate;
  199. static int    yyHaveDay;
  200. static int    yyHaveRel;
  201. static int    yyHaveTime;
  202. static int    yyHaveZone;
  203. static time_t    yyTimezone;
  204. static time_t    yyDay;
  205. static time_t    yyHour;
  206. static time_t    yyMinutes;
  207. static time_t    yyMonth;
  208. static time_t    yySeconds;
  209. static time_t    yyYear;
  210. static MERIDIAN    yyMeridian;
  211. static time_t    yyRelMonth;
  212. static time_t    yyRelSeconds;
  213.  
  214. %}
  215.  
  216. %union {
  217.     time_t        Number;
  218.     enum _MERIDIAN    Meridian;
  219. }
  220.  
  221. %token    tAGO tDAY tDAYZONE tID tMERIDIAN tMINUTE_UNIT tMONTH tMONTH_UNIT
  222. %token    tSEC_UNIT tSNUMBER tUNUMBER tZONE tDST
  223.  
  224. %type    <Number>    tDAY tDAYZONE tMINUTE_UNIT tMONTH tMONTH_UNIT
  225. %type    <Number>    tSEC_UNIT tSNUMBER tUNUMBER tZONE
  226. %type    <Meridian>    tMERIDIAN o_merid
  227.  
  228. %%
  229.  
  230. spec    : /* NULL */
  231.     | spec item
  232.     ;
  233.  
  234. item    : time {
  235.         yyHaveTime++;
  236.     }
  237.     | zone {
  238.         yyHaveZone++;
  239.     }
  240.     | date {
  241.         yyHaveDate++;
  242.     }
  243.     | day {
  244.         yyHaveDay++;
  245.     }
  246.     | rel {
  247.         yyHaveRel++;
  248.     }
  249.     | number
  250.     ;
  251.  
  252. time    : tUNUMBER tMERIDIAN {
  253.         yyHour = $1;
  254.         yyMinutes = 0;
  255.         yySeconds = 0;
  256.         yyMeridian = $2;
  257.     }
  258.     | tUNUMBER ':' tUNUMBER o_merid {
  259.         yyHour = $1;
  260.         yyMinutes = $3;
  261.         yySeconds = 0;
  262.         yyMeridian = $4;
  263.     }
  264.     | tUNUMBER ':' tUNUMBER tSNUMBER {
  265.         yyHour = $1;
  266.         yyMinutes = $3;
  267.         yyMeridian = MER24;
  268.         yyDSTmode = DSToff;
  269.         yyTimezone = - ($4 % 100 + ($4 / 100) * 60);
  270.     }
  271.     | tUNUMBER ':' tUNUMBER ':' tUNUMBER o_merid {
  272.         yyHour = $1;
  273.         yyMinutes = $3;
  274.         yySeconds = $5;
  275.         yyMeridian = $6;
  276.     }
  277.     | tUNUMBER ':' tUNUMBER ':' tUNUMBER tSNUMBER {
  278.         yyHour = $1;
  279.         yyMinutes = $3;
  280.         yySeconds = $5;
  281.         yyMeridian = MER24;
  282.         yyDSTmode = DSToff;
  283.         yyTimezone = - ($6 % 100 + ($6 / 100) * 60);
  284.     }
  285.     ;
  286.  
  287. zone    : tZONE {
  288.         yyTimezone = $1;
  289.         yyDSTmode = DSToff;
  290.     }
  291.     | tDAYZONE {
  292.         yyTimezone = $1;
  293.         yyDSTmode = DSTon;
  294.     }
  295.     |
  296.       tZONE tDST {
  297.         yyTimezone = $1;
  298.         yyDSTmode = DSTon;
  299.     }
  300.     ;
  301.  
  302. day    : tDAY {
  303.         yyDayOrdinal = 1;
  304.         yyDayNumber = $1;
  305.     }
  306.     | tDAY ',' {
  307.         yyDayOrdinal = 1;
  308.         yyDayNumber = $1;
  309.     }
  310.     | tUNUMBER tDAY {
  311.         yyDayOrdinal = $1;
  312.         yyDayNumber = $2;
  313.     }
  314.     ;
  315.  
  316. date    : tUNUMBER '/' tUNUMBER {
  317.         yyMonth = $1;
  318.         yyDay = $3;
  319.     }
  320.     | tUNUMBER '/' tUNUMBER '/' tUNUMBER {
  321.         yyMonth = $1;
  322.         yyDay = $3;
  323.         yyYear = $5;
  324.     }
  325.     | tUNUMBER tSNUMBER tSNUMBER {
  326.         /* ISO 8601 format.  yyyy-mm-dd.  */
  327.         yyYear = $1;
  328.         yyMonth = -$2;
  329.         yyDay = -$3;
  330.     }
  331.     | tMONTH tUNUMBER {
  332.         yyMonth = $1;
  333.         yyDay = $2;
  334.     }
  335.     | tMONTH tUNUMBER ',' tUNUMBER {
  336.         yyMonth = $1;
  337.         yyDay = $2;
  338.         yyYear = $4;
  339.     }
  340.     | tUNUMBER tMONTH {
  341.         yyMonth = $2;
  342.         yyDay = $1;
  343.     }
  344.     | tUNUMBER tMONTH tUNUMBER {
  345.         yyMonth = $2;
  346.         yyDay = $1;
  347.         yyYear = $3;
  348.     }
  349.     ;
  350.  
  351. rel    : relunit tAGO {
  352.         yyRelSeconds = -yyRelSeconds;
  353.         yyRelMonth = -yyRelMonth;
  354.     }
  355.     | relunit
  356.     ;
  357.  
  358. relunit    : tUNUMBER tMINUTE_UNIT {
  359.         yyRelSeconds += $1 * $2 * 60L;
  360.     }
  361.     | tSNUMBER tMINUTE_UNIT {
  362.         yyRelSeconds += $1 * $2 * 60L;
  363.     }
  364.     | tMINUTE_UNIT {
  365.         yyRelSeconds += $1 * 60L;
  366.     }
  367.     | tSNUMBER tSEC_UNIT {
  368.         yyRelSeconds += $1;
  369.     }
  370.     | tUNUMBER tSEC_UNIT {
  371.         yyRelSeconds += $1;
  372.     }
  373.     | tSEC_UNIT {
  374.         yyRelSeconds++;
  375.     }
  376.     | tSNUMBER tMONTH_UNIT {
  377.         yyRelMonth += $1 * $2;
  378.     }
  379.     | tUNUMBER tMONTH_UNIT {
  380.         yyRelMonth += $1 * $2;
  381.     }
  382.     | tMONTH_UNIT {
  383.         yyRelMonth += $1;
  384.     }
  385.     ;
  386.  
  387. number    : tUNUMBER {
  388.         if (yyHaveTime && yyHaveDate && !yyHaveRel)
  389.         yyYear = $1;
  390.         else {
  391.         if($1>10000) {
  392.             time_t date_part;
  393.  
  394.             date_part= $1/10000;
  395.             yyHaveDate++;
  396.             yyDay= (date_part)%100;
  397.             yyMonth= (date_part/100)%100;
  398.             yyYear = date_part/10000;
  399.         } 
  400.             yyHaveTime++;
  401.         if ($1 < 100) {
  402.             yyHour = $1;
  403.             yyMinutes = 0;
  404.         }
  405.         else {
  406.             yyHour = $1 / 100;
  407.             yyMinutes = $1 % 100;
  408.         }
  409.         yySeconds = 0;
  410.         yyMeridian = MER24;
  411.         }
  412.     }
  413.     ;
  414.  
  415. o_merid    : /* NULL */ {
  416.         $$ = MER24;
  417.     }
  418.     | tMERIDIAN {
  419.         $$ = $1;
  420.     }
  421.     ;
  422.  
  423. %%
  424.  
  425. /* Month and day table. */
  426. static TABLE const MonthDayTable[] = {
  427.     { "january",    tMONTH,  1 },
  428.     { "february",    tMONTH,  2 },
  429.     { "march",        tMONTH,  3 },
  430.     { "april",        tMONTH,  4 },
  431.     { "may",        tMONTH,  5 },
  432.     { "june",        tMONTH,  6 },
  433.     { "july",        tMONTH,  7 },
  434.     { "august",        tMONTH,  8 },
  435.     { "september",    tMONTH,  9 },
  436.     { "sept",        tMONTH,  9 },
  437.     { "october",    tMONTH, 10 },
  438.     { "november",    tMONTH, 11 },
  439.     { "december",    tMONTH, 12 },
  440.     { "sunday",        tDAY, 0 },
  441.     { "monday",        tDAY, 1 },
  442.     { "tuesday",    tDAY, 2 },
  443.     { "tues",        tDAY, 2 },
  444.     { "wednesday",    tDAY, 3 },
  445.     { "wednes",        tDAY, 3 },
  446.     { "thursday",    tDAY, 4 },
  447.     { "thur",        tDAY, 4 },
  448.     { "thurs",        tDAY, 4 },
  449.     { "friday",        tDAY, 5 },
  450.     { "saturday",    tDAY, 6 },
  451.     { NULL }
  452. };
  453.  
  454. /* Time units table. */
  455. static TABLE const UnitsTable[] = {
  456.     { "year",        tMONTH_UNIT,    12 },
  457.     { "month",        tMONTH_UNIT,    1 },
  458.     { "fortnight",    tMINUTE_UNIT,    14 * 24 * 60 },
  459.     { "week",        tMINUTE_UNIT,    7 * 24 * 60 },
  460.     { "day",        tMINUTE_UNIT,    1 * 24 * 60 },
  461.     { "hour",        tMINUTE_UNIT,    60 },
  462.     { "minute",        tMINUTE_UNIT,    1 },
  463.     { "min",        tMINUTE_UNIT,    1 },
  464.     { "second",        tSEC_UNIT,    1 },
  465.     { "sec",        tSEC_UNIT,    1 },
  466.     { NULL }
  467. };
  468.  
  469. /* Assorted relative-time words. */
  470. static TABLE const OtherTable[] = {
  471.     { "tomorrow",    tMINUTE_UNIT,    1 * 24 * 60 },
  472.     { "yesterday",    tMINUTE_UNIT,    -1 * 24 * 60 },
  473.     { "today",        tMINUTE_UNIT,    0 },
  474.     { "now",        tMINUTE_UNIT,    0 },
  475.     { "last",        tUNUMBER,    -1 },
  476.     { "this",        tMINUTE_UNIT,    0 },
  477.     { "next",        tUNUMBER,    2 },
  478.     { "first",        tUNUMBER,    1 },
  479. /*  { "second",        tUNUMBER,    2 }, */
  480.     { "third",        tUNUMBER,    3 },
  481.     { "fourth",        tUNUMBER,    4 },
  482.     { "fifth",        tUNUMBER,    5 },
  483.     { "sixth",        tUNUMBER,    6 },
  484.     { "seventh",    tUNUMBER,    7 },
  485.     { "eighth",        tUNUMBER,    8 },
  486.     { "ninth",        tUNUMBER,    9 },
  487.     { "tenth",        tUNUMBER,    10 },
  488.     { "eleventh",    tUNUMBER,    11 },
  489.     { "twelfth",    tUNUMBER,    12 },
  490.     { "ago",        tAGO,    1 },
  491.     { NULL }
  492. };
  493.  
  494. /* The timezone table. */
  495. /* Some of these are commented out because a time_t can't store a float. */
  496. static TABLE const TimezoneTable[] = {
  497.     { "gmt",    tZONE,     HOUR( 0) },    /* Greenwich Mean */
  498.     { "ut",    tZONE,     HOUR( 0) },    /* Universal (Coordinated) */
  499.     { "utc",    tZONE,     HOUR( 0) },
  500.     { "wet",    tZONE,     HOUR( 0) },    /* Western European */
  501.     { "bst",    tDAYZONE,  HOUR( 0) },    /* British Summer */
  502.     { "wat",    tZONE,     HOUR( 1) },    /* West Africa */
  503.     { "at",    tZONE,     HOUR( 2) },    /* Azores */
  504. #if    0
  505.     /* For completeness.  BST is also British Summer, and GST is
  506.      * also Guam Standard. */
  507.     { "bst",    tZONE,     HOUR( 3) },    /* Brazil Standard */
  508.     { "gst",    tZONE,     HOUR( 3) },    /* Greenland Standard */
  509. #endif
  510. #if 0
  511.     { "nft",    tZONE,     HOUR(3.5) },    /* Newfoundland */
  512.     { "nst",    tZONE,     HOUR(3.5) },    /* Newfoundland Standard */
  513.     { "ndt",    tDAYZONE,  HOUR(3.5) },    /* Newfoundland Daylight */
  514. #endif
  515.     { "ast",    tZONE,     HOUR( 4) },    /* Atlantic Standard */
  516.     { "adt",    tDAYZONE,  HOUR( 4) },    /* Atlantic Daylight */
  517.     { "est",    tZONE,     HOUR( 5) },    /* Eastern Standard */
  518.     { "edt",    tDAYZONE,  HOUR( 5) },    /* Eastern Daylight */
  519.     { "cst",    tZONE,     HOUR( 6) },    /* Central Standard */
  520.     { "cdt",    tDAYZONE,  HOUR( 6) },    /* Central Daylight */
  521.     { "mst",    tZONE,     HOUR( 7) },    /* Mountain Standard */
  522.     { "mdt",    tDAYZONE,  HOUR( 7) },    /* Mountain Daylight */
  523.     { "pst",    tZONE,     HOUR( 8) },    /* Pacific Standard */
  524.     { "pdt",    tDAYZONE,  HOUR( 8) },    /* Pacific Daylight */
  525.     { "yst",    tZONE,     HOUR( 9) },    /* Yukon Standard */
  526.     { "ydt",    tDAYZONE,  HOUR( 9) },    /* Yukon Daylight */
  527.     { "hst",    tZONE,     HOUR(10) },    /* Hawaii Standard */
  528.     { "hdt",    tDAYZONE,  HOUR(10) },    /* Hawaii Daylight */
  529.     { "cat",    tZONE,     HOUR(10) },    /* Central Alaska */
  530.     { "ahst",    tZONE,     HOUR(10) },    /* Alaska-Hawaii Standard */
  531.     { "nt",    tZONE,     HOUR(11) },    /* Nome */
  532.     { "idlw",    tZONE,     HOUR(12) },    /* International Date Line West */
  533.     { "cet",    tZONE,     -HOUR(1) },    /* Central European */
  534.     { "met",    tZONE,     -HOUR(1) },    /* Middle European */
  535.     { "mewt",    tZONE,     -HOUR(1) },    /* Middle European Winter */
  536.     { "mest",    tDAYZONE,  -HOUR(1) },    /* Middle European Summer */
  537.     { "swt",    tZONE,     -HOUR(1) },    /* Swedish Winter */
  538.     { "sst",    tDAYZONE,  -HOUR(1) },    /* Swedish Summer */
  539.     { "fwt",    tZONE,     -HOUR(1) },    /* French Winter */
  540.     { "fst",    tDAYZONE,  -HOUR(1) },    /* French Summer */
  541.     { "eet",    tZONE,     -HOUR(2) },    /* Eastern Europe, USSR Zone 1 */
  542.     { "bt",    tZONE,     -HOUR(3) },    /* Baghdad, USSR Zone 2 */
  543. #if 0
  544.     { "it",    tZONE,     -HOUR(3.5) },/* Iran */
  545. #endif
  546.     { "zp4",    tZONE,     -HOUR(4) },    /* USSR Zone 3 */
  547.     { "zp5",    tZONE,     -HOUR(5) },    /* USSR Zone 4 */
  548. #if 0
  549.     { "ist",    tZONE,     -HOUR(5.5) },/* Indian Standard */
  550. #endif
  551.     { "zp6",    tZONE,     -HOUR(6) },    /* USSR Zone 5 */
  552. #if    0
  553.     /* For completeness.  NST is also Newfoundland Stanard, and SST is
  554.      * also Swedish Summer. */
  555.     { "nst",    tZONE,     -HOUR(6.5) },/* North Sumatra */
  556.     { "sst",    tZONE,     -HOUR(7) },    /* South Sumatra, USSR Zone 6 */
  557. #endif    /* 0 */
  558.     { "wast",    tZONE,     -HOUR(7) },    /* West Australian Standard */
  559.     { "wadt",    tDAYZONE,  -HOUR(7) },    /* West Australian Daylight */
  560. #if 0
  561.     { "jt",    tZONE,     -HOUR(7.5) },/* Java (3pm in Cronusland!) */
  562. #endif
  563.     { "cct",    tZONE,     -HOUR(8) },    /* China Coast, USSR Zone 7 */
  564.     { "jst",    tZONE,     -HOUR(9) },    /* Japan Standard, USSR Zone 8 */
  565. #if 0
  566.     { "cast",    tZONE,     -HOUR(9.5) },/* Central Australian Standard */
  567.     { "cadt",    tDAYZONE,  -HOUR(9.5) },/* Central Australian Daylight */
  568. #endif
  569.     { "east",    tZONE,     -HOUR(10) },    /* Eastern Australian Standard */
  570.     { "eadt",    tDAYZONE,  -HOUR(10) },    /* Eastern Australian Daylight */
  571.     { "gst",    tZONE,     -HOUR(10) },    /* Guam Standard, USSR Zone 9 */
  572.     { "nzt",    tZONE,     -HOUR(12) },    /* New Zealand */
  573.     { "nzst",    tZONE,     -HOUR(12) },    /* New Zealand Standard */
  574.     { "nzdt",    tDAYZONE,  -HOUR(12) },    /* New Zealand Daylight */
  575.     { "idle",    tZONE,     -HOUR(12) },    /* International Date Line East */
  576.     {  NULL  }
  577. };
  578.  
  579. /* Military timezone table. */
  580. static TABLE const MilitaryTable[] = {
  581.     { "a",    tZONE,    HOUR(  1) },
  582.     { "b",    tZONE,    HOUR(  2) },
  583.     { "c",    tZONE,    HOUR(  3) },
  584.     { "d",    tZONE,    HOUR(  4) },
  585.     { "e",    tZONE,    HOUR(  5) },
  586.     { "f",    tZONE,    HOUR(  6) },
  587.     { "g",    tZONE,    HOUR(  7) },
  588.     { "h",    tZONE,    HOUR(  8) },
  589.     { "i",    tZONE,    HOUR(  9) },
  590.     { "k",    tZONE,    HOUR( 10) },
  591.     { "l",    tZONE,    HOUR( 11) },
  592.     { "m",    tZONE,    HOUR( 12) },
  593.     { "n",    tZONE,    HOUR(- 1) },
  594.     { "o",    tZONE,    HOUR(- 2) },
  595.     { "p",    tZONE,    HOUR(- 3) },
  596.     { "q",    tZONE,    HOUR(- 4) },
  597.     { "r",    tZONE,    HOUR(- 5) },
  598.     { "s",    tZONE,    HOUR(- 6) },
  599.     { "t",    tZONE,    HOUR(- 7) },
  600.     { "u",    tZONE,    HOUR(- 8) },
  601.     { "v",    tZONE,    HOUR(- 9) },
  602.     { "w",    tZONE,    HOUR(-10) },
  603.     { "x",    tZONE,    HOUR(-11) },
  604.     { "y",    tZONE,    HOUR(-12) },
  605.     { "z",    tZONE,    HOUR(  0) },
  606.     { NULL }
  607. };
  608.  
  609.  
  610.  
  611.  
  612. /* ARGSUSED */
  613. static int
  614. yyerror(s)
  615.     char    *s;
  616. {
  617.   return 0;
  618. }
  619.  
  620.  
  621. static time_t
  622. ToSeconds(Hours, Minutes, Seconds, Meridian)
  623.     time_t    Hours;
  624.     time_t    Minutes;
  625.     time_t    Seconds;
  626.     MERIDIAN    Meridian;
  627. {
  628.     if (Minutes < 0 || Minutes > 59 || Seconds < 0 || Seconds > 59)
  629.     return -1;
  630.     switch (Meridian) {
  631.     case MER24:
  632.     if (Hours < 0 || Hours > 23)
  633.         return -1;
  634.     return (Hours * 60L + Minutes) * 60L + Seconds;
  635.     case MERam:
  636.     if (Hours < 1 || Hours > 12)
  637.         return -1;
  638.     return (Hours * 60L + Minutes) * 60L + Seconds;
  639.     case MERpm:
  640.     if (Hours < 1 || Hours > 12)
  641.         return -1;
  642.     return ((Hours + 12) * 60L + Minutes) * 60L + Seconds;
  643.     }
  644.     /* NOTREACHED */
  645. }
  646.  
  647.  
  648. static time_t
  649. Convert(Month, Day, Year, Hours, Minutes, Seconds, Meridian, DSTmode)
  650.     time_t    Month;
  651.     time_t    Day;
  652.     time_t    Year;
  653.     time_t    Hours;
  654.     time_t    Minutes;
  655.     time_t    Seconds;
  656.     MERIDIAN    Meridian;
  657.     DSTMODE    DSTmode;
  658. {
  659.     static int DaysInMonth[12] = {
  660.     31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  661.     };
  662.     time_t    tod;
  663.     time_t    Julian;
  664.     int        i;
  665.  
  666.     if (Year < 0)
  667.     Year = -Year;
  668.     if (Year < 100)
  669.     Year += 1900;
  670.     DaysInMonth[1] = Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0)
  671.             ? 29 : 28;
  672.     if (Year < EPOCH || Year > 1999
  673.      || Month < 1 || Month > 12
  674.      /* Lint fluff:  "conversion from long may lose accuracy" */
  675.      || Day < 1 || Day > DaysInMonth[(int)--Month])
  676.     return -1;
  677.  
  678.     for (Julian = Day - 1, i = 0; i < Month; i++)
  679.     Julian += DaysInMonth[i];
  680.     for (i = EPOCH; i < Year; i++)
  681.     Julian += 365 + (i % 4 == 0);
  682.     Julian *= SECSPERDAY;
  683.     Julian += yyTimezone * 60L;
  684.     if ((tod = ToSeconds(Hours, Minutes, Seconds, Meridian)) < 0)
  685.     return -1;
  686.     Julian += tod;
  687.     if (DSTmode == DSTon
  688.      || (DSTmode == DSTmaybe && localtime(&Julian)->tm_isdst))
  689.     Julian -= 60 * 60;
  690.     return Julian;
  691. }
  692.  
  693.  
  694. static time_t
  695. DSTcorrect(Start, Future)
  696.     time_t    Start;
  697.     time_t    Future;
  698. {
  699.     time_t    StartDay;
  700.     time_t    FutureDay;
  701.  
  702.     StartDay = (localtime(&Start)->tm_hour + 1) % 24;
  703.     FutureDay = (localtime(&Future)->tm_hour + 1) % 24;
  704.     return (Future - Start) + (StartDay - FutureDay) * 60L * 60L;
  705. }
  706.  
  707.  
  708. static time_t
  709. RelativeDate(Start, DayOrdinal, DayNumber)
  710.     time_t    Start;
  711.     time_t    DayOrdinal;
  712.     time_t    DayNumber;
  713. {
  714.     struct tm    *tm;
  715.     time_t    now;
  716.  
  717.     now = Start;
  718.     tm = localtime(&now);
  719.     now += SECSPERDAY * ((DayNumber - tm->tm_wday + 7) % 7);
  720.     now += 7 * SECSPERDAY * (DayOrdinal <= 0 ? DayOrdinal : DayOrdinal - 1);
  721.     return DSTcorrect(Start, now);
  722. }
  723.  
  724.  
  725. static time_t
  726. RelativeMonth(Start, RelMonth)
  727.     time_t    Start;
  728.     time_t    RelMonth;
  729. {
  730.     struct tm    *tm;
  731.     time_t    Month;
  732.     time_t    Year;
  733.  
  734.     if (RelMonth == 0)
  735.     return 0;
  736.     tm = localtime(&Start);
  737.     Month = 12 * tm->tm_year + tm->tm_mon + RelMonth;
  738.     Year = Month / 12;
  739.     Month = Month % 12 + 1;
  740.     return DSTcorrect(Start,
  741.         Convert(Month, (time_t)tm->tm_mday, Year,
  742.         (time_t)tm->tm_hour, (time_t)tm->tm_min, (time_t)tm->tm_sec,
  743.         MER24, DSTmaybe));
  744. }
  745.  
  746.  
  747. static int
  748. LookupWord(buff)
  749.     char        *buff;
  750. {
  751.     register char    *p;
  752.     register char    *q;
  753.     register const TABLE    *tp;
  754.     int            i;
  755.     int            abbrev;
  756.  
  757.     /* Make it lowercase. */
  758.     for (p = buff; *p; p++)
  759.     if (isupper(*p))
  760.         *p = tolower(*p);
  761.  
  762.     if (strcmp(buff, "am") == 0 || strcmp(buff, "a.m.") == 0) {
  763.     yylval.Meridian = MERam;
  764.     return tMERIDIAN;
  765.     }
  766.     if (strcmp(buff, "pm") == 0 || strcmp(buff, "p.m.") == 0) {
  767.     yylval.Meridian = MERpm;
  768.     return tMERIDIAN;
  769.     }
  770.  
  771.     /* See if we have an abbreviation for a month. */
  772.     if (strlen(buff) == 3)
  773.     abbrev = 1;
  774.     else if (strlen(buff) == 4 && buff[3] == '.') {
  775.     abbrev = 1;
  776.     buff[3] = '\0';
  777.     }
  778.     else
  779.     abbrev = 0;
  780.  
  781.     for (tp = MonthDayTable; tp->name; tp++) {
  782.     if (abbrev) {
  783.         if (strncmp(buff, tp->name, 3) == 0) {
  784.         yylval.Number = tp->value;
  785.         return tp->type;
  786.         }
  787.     }
  788.     else if (strcmp(buff, tp->name) == 0) {
  789.         yylval.Number = tp->value;
  790.         return tp->type;
  791.     }
  792.     }
  793.  
  794.     for (tp = TimezoneTable; tp->name; tp++)
  795.     if (strcmp(buff, tp->name) == 0) {
  796.         yylval.Number = tp->value;
  797.         return tp->type;
  798.     }
  799.  
  800.     if (strcmp(buff, "dst") == 0) 
  801.     return tDST;
  802.  
  803.     for (tp = UnitsTable; tp->name; tp++)
  804.     if (strcmp(buff, tp->name) == 0) {
  805.         yylval.Number = tp->value;
  806.         return tp->type;
  807.     }
  808.  
  809.     /* Strip off any plural and try the units table again. */
  810.     i = strlen(buff) - 1;
  811.     if (buff[i] == 's') {
  812.     buff[i] = '\0';
  813.     for (tp = UnitsTable; tp->name; tp++)
  814.         if (strcmp(buff, tp->name) == 0) {
  815.         yylval.Number = tp->value;
  816.         return tp->type;
  817.         }
  818.     buff[i] = 's';        /* Put back for "this" in OtherTable. */
  819.     }
  820.  
  821.     for (tp = OtherTable; tp->name; tp++)
  822.     if (strcmp(buff, tp->name) == 0) {
  823.         yylval.Number = tp->value;
  824.         return tp->type;
  825.     }
  826.  
  827.     /* Military timezones. */
  828.     if (buff[1] == '\0' && isalpha(*buff)) {
  829.     for (tp = MilitaryTable; tp->name; tp++)
  830.         if (strcmp(buff, tp->name) == 0) {
  831.         yylval.Number = tp->value;
  832.         return tp->type;
  833.         }
  834.     }
  835.  
  836.     /* Drop out any periods and try the timezone table again. */
  837.     for (i = 0, p = q = buff; *q; q++)
  838.     if (*q != '.')
  839.         *p++ = *q;
  840.     else
  841.         i++;
  842.     *p = '\0';
  843.     if (i)
  844.     for (tp = TimezoneTable; tp->name; tp++)
  845.         if (strcmp(buff, tp->name) == 0) {
  846.         yylval.Number = tp->value;
  847.         return tp->type;
  848.         }
  849.  
  850.     return tID;
  851. }
  852.  
  853.  
  854. static int
  855. yylex()
  856. {
  857.     register char    c;
  858.     register char    *p;
  859.     char        buff[20];
  860.     int            Count;
  861.     int            sign;
  862.  
  863.     for ( ; ; ) {
  864.     while (isspace(*yyInput))
  865.         yyInput++;
  866.  
  867.     if (isdigit(c = *yyInput) || c == '-' || c == '+') {
  868.         if (c == '-' || c == '+') {
  869.         sign = c == '-' ? -1 : 1;
  870.         if (!isdigit(*++yyInput))
  871.             /* skip the '-' sign */
  872.             continue;
  873.         }
  874.         else
  875.         sign = 0;
  876.         for (yylval.Number = 0; isdigit(c = *yyInput++); )
  877.         yylval.Number = 10 * yylval.Number + c - '0';
  878.         yyInput--;
  879.         if (sign < 0)
  880.         yylval.Number = -yylval.Number;
  881.         return sign ? tSNUMBER : tUNUMBER;
  882.     }
  883.     if (isalpha(c)) {
  884.         for (p = buff; isalpha(c = *yyInput++) || c == '.'; )
  885.         if (p < &buff[sizeof buff - 1])
  886.             *p++ = c;
  887.         *p = '\0';
  888.         yyInput--;
  889.         return LookupWord(buff);
  890.     }
  891.     if (c != '(')
  892.         return *yyInput++;
  893.     Count = 0;
  894.     do {
  895.         c = *yyInput++;
  896.         if (c == '\0')
  897.         return c;
  898.         if (c == '(')
  899.         Count++;
  900.         else if (c == ')')
  901.         Count--;
  902.     } while (Count > 0);
  903.     }
  904. }
  905.  
  906.  
  907. time_t
  908. get_date(p, now)
  909.     char        *p;
  910.     struct timeb    *now;
  911. {
  912.     struct tm        *tm;
  913.     struct timeb    ftz;
  914.     time_t        Start;
  915.     time_t        tod;
  916.  
  917.     yyInput = p;
  918.     if (now == NULL) {
  919.         now = &ftz;
  920. #if    !defined(HAVE_FTIME)
  921.     (void)time(&ftz.time);
  922.     /* Set the timezone global. */
  923.     tzset();
  924.     {
  925. #ifdef HAVE_GETTIMEOFDAY
  926.         struct timeval tv;
  927.         struct timezone tz;
  928.  
  929.         gettimeofday (&tv, &tz);
  930.         ftz.timezone = (int) tz.tz_minuteswest;
  931. #else /* not HAVE_GETTIMEOFDAY */ 
  932. #if sgi
  933.         ftz.timezone = (int) _timezone / 60;
  934. #else /* not HAVE_GETTIMEOFDAY, nor sgi */
  935. #ifdef __386BSD__
  936.         ftz.timezone = 0;
  937. #else /* not HAVE_GETTIMEOFDAY, nor sgi, nor 386BSD -- probably USG */
  938.         extern time_t timezone;
  939.  
  940.         ftz.timezone = (int) timezone / 60;
  941. #endif /* not HAVE_GETTIMEOFDAY, nor sgi, nor 386BSD -- probably USG */
  942. #endif /* not HAVE_GETTIMEOFDAY, nor sgi */
  943. #endif /* not HAVE_GETTIMEOFDAY */
  944.     }
  945. #else /* HAVE_FTIME */
  946.     (void)ftime(&ftz);
  947. #endif /* HAVE_FTIME */
  948.     }
  949.  
  950.     tm = localtime(&now->time);
  951.     yyYear = tm->tm_year;
  952.     yyMonth = tm->tm_mon + 1;
  953.     yyDay = tm->tm_mday;
  954.     yyTimezone = now->timezone;
  955.     yyDSTmode = DSTmaybe;
  956.     yyHour = 0;
  957.     yyMinutes = 0;
  958.     yySeconds = 0;
  959.     yyMeridian = MER24;
  960.     yyRelSeconds = 0;
  961.     yyRelMonth = 0;
  962.     yyHaveDate = 0;
  963.     yyHaveDay = 0;
  964.     yyHaveRel = 0;
  965.     yyHaveTime = 0;
  966.     yyHaveZone = 0;
  967.  
  968.     if (yyparse()
  969.      || yyHaveTime > 1 || yyHaveZone > 1 || yyHaveDate > 1 || yyHaveDay > 1)
  970.     return -1;
  971.  
  972.     if (yyHaveDate || yyHaveTime || yyHaveDay) {
  973.     Start = Convert(yyMonth, yyDay, yyYear, yyHour, yyMinutes, yySeconds,
  974.             yyMeridian, yyDSTmode);
  975.     if (Start < 0)
  976.         return -1;
  977.     }
  978.     else {
  979.     Start = now->time;
  980.     if (!yyHaveRel)
  981.         Start -= ((tm->tm_hour * 60L + tm->tm_min) * 60L) + tm->tm_sec;
  982.     }
  983.  
  984.     Start += yyRelSeconds;
  985.     Start += RelativeMonth(Start, yyRelMonth);
  986.  
  987.     if (yyHaveDay && !yyHaveDate) {
  988.     tod = RelativeDate(Start, yyDayOrdinal, yyDayNumber);
  989.     Start += tod;
  990.     }
  991.  
  992.     /* Have to do *something* with a legitimate -1 so it's distinguishable
  993.      * from the error return value.  (Alternately could set errno on error.) */
  994.     return Start == -1 ? 0 : Start;
  995. }
  996.  
  997.  
  998. #if    defined(TEST)
  999.  
  1000. /* ARGSUSED */
  1001. main(ac, av)
  1002.     int        ac;
  1003.     char    *av[];
  1004. {
  1005.     char    buff[128];
  1006.     time_t    d;
  1007.  
  1008.     (void)printf("Enter date, or blank line to exit.\n\t> ");
  1009.     (void)fflush(stdout);
  1010.     while (gets(buff) && buff[0]) {
  1011.     d = get_date(buff, (struct timeb *)NULL);
  1012.     if (d == -1)
  1013.         (void)printf("Bad format - couldn't convert.\n");
  1014.     else
  1015.         (void)printf("%s", ctime(&d));
  1016.     (void)printf("\t> ");
  1017.     (void)fflush(stdout);
  1018.     }
  1019.     exit(0);
  1020.     /* NOTREACHED */
  1021. }
  1022. #endif    /* defined(TEST) */
  1023.